package cxt.com.recyclerview.demo5;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cxt.com.recyclerview.R;

/**
 * RecycleView 实现设置setEmptyView()
 */
public class MainActivity5 extends AppCompatActivity {
    private EmptyRecycleView mRecyclerView;
    private NormalAdapter mNormalAdapter;
    private List<String> mData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        initView();
    }


    private void initView() {
        mRecyclerView = (EmptyRecycleView) findViewById(R.id.emptyRecycleView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mData = new ArrayList<>();
        mNormalAdapter = new NormalAdapter(mData);
        View view = findViewById(R.id.text_empty);
        mRecyclerView.setmEmptyView(view);
        mRecyclerView.setAdapter(mNormalAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_5, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_add:
                mData.add(0, "hello");
                mNormalAdapter.notifyItemChanged(0);
                break;
            case R.id.item_delete:
                if (!mData.isEmpty()) {
                    mData.remove(0);
                    mNormalAdapter.notifyItemRemoved(0);
                }
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
